package com.gongJiao.service.impl;

import com.gongJiao.entity.RouteSite;
import com.gongJiao.mapper.RouteSiteMapper;
import com.gongJiao.service.RouteSiteService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ${author}
 * @since 2021-12-25
 */
@Service
public class RouteSiteServiceImpl extends ServiceImpl<RouteSiteMapper, RouteSite> implements RouteSiteService {

    @Autowired
    RouteSiteMapper routeSiteMapper;
    /**
     * 根据路线id查询关联站点id字符串
     * @param id
     * @return
     */
    @Override
    public String getSitesByRoute(Long id) {
        return routeSiteMapper.getSitesByRoute(id);
    }

    /**
     * 跟据路线id修改路线站点信息
     * @param id
     * @param xmselectName
     */
    @Override
    public void updateByRouteId(Long id, String xmselectName) {
        String[] split = xmselectName.split(",");
        int[] array = Arrays.stream(split).mapToInt(Integer::parseInt).toArray();
        //删除数据
        routeSiteMapper.deleteByRouteId(id);
        //添加新数据
        for (int i = 0; i < array.length; i++) {
            RouteSite routeSite = new RouteSite();
            routeSite.setRouteId(id);
            routeSite.setSiteId((long) array[i]);
            routeSite.setNumber(i);
            routeSiteMapper.insert(routeSite);
        }
    }
}
